package com.d2.modules.system.permission.util;

import com.d2.modules.system.Constant;
import com.d2.modules.system.permission.entity.User;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.Serializable;

/**
 * @author mayihua
 */
public class UserContext implements Serializable {

    /**
     * 获取当前线程绑定的用户登录对象
     *
     * @return
     */
    public static User get() {
        return (User) RequestContextHolder.getRequestAttributes().getAttribute(Constant.LOGIN_KEY,  RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * 将用户登录对象绑定到当前线程
     *
     * @param User
     */
    public static void set(User User) {
        RequestContextHolder.getRequestAttributes().setAttribute(Constant.LOGIN_KEY, User, RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * 将用户登录对象从当前线程销毁
     */
    public static void remove() {
        RequestContextHolder.getRequestAttributes().removeAttribute(Constant.LOGIN_KEY,RequestAttributes.SCOPE_REQUEST);
    }

}
